package com.ydc.ln.tutorial;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;

import com.ydc.ln.commons.utils.PropertiesUtil;


/**
 * 
 * 读取Properties配置文件信息
 * 
 * @author sunlightcs
 * 
 * 2011-5-2
 * 
 * http://hi.juziku.com/sunlightcs/
 * 
 */

public class PropertiesTest {

	private static Logger logger = Logger.getLogger(PropertiesTest.class);
	
	private PropertiesUtil propsUtil = null;
	
	@Before
	public void setUp() {
		propsUtil = new PropertiesUtil("db.properties");
	}
	
	@Test
	public void testGetProperty() {
		String key = "db.url";
		String value = propsUtil.getProperty(key);
		System.out.println(value);
	}
	
	/**
	 * 
	 * 读取Properties配置文件
	 * 
	 * @return 返回Properties数据
	 * 
	 */
	public Properties getPropertiesFile() {
		InputStream is = this.getClass().getClassLoader().getResourceAsStream("config.properties");
		Properties prop = new Properties();
		try {
			prop.load(is);
			is.close();
		} catch (IOException ex) {
			logger.error("读取配置文件失败，配置文件路径为：config.properties", ex);
		}
		return prop;
	}

	/**
	 * 
	 * 读取Properties配置文件
	 * 
	 * @param filePath
	 *            配置文件路径
	 * 
	 * @return 返回Properties数据
	 * 
	 */

	public Properties getPropertiesFile(String filePath) {
		InputStream is = this.getClass().getClassLoader().getResourceAsStream(filePath);
		Properties prop = new Properties();
		try {
			prop.load(is);
			is.close();
		} catch (IOException ex) {
			logger.error("读取配置文件失败，配置文件路径为：" + filePath, ex);
		}
		return prop;
	}

	public static void main(String[] args) {

		Properties prop = new PropertiesTest().getPropertiesFile();

		String path = prop.getProperty("imagePath");

		System.out.println(path);

	}

}
